home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / ImageMap.java < prev    next >
Text File  |  1998-09-15  |  14KB  |  489 lines

  1. /*
  2.  * @(#)ImageMap.java    1.12 98/03/23
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.applet.Applet;
  32. import java.awt.Image;
  33. import java.awt.Graphics;
  34. import java.awt.Rectangle;
  35. import java.awt.MediaTracker;
  36. import java.awt.event.*;
  37. import java.util.StringTokenizer;
  38. import java.util.Vector;
  39. import java.util.Hashtable;
  40. import java.net.URL;
  41. import java.awt.image.ImageProducer;
  42. import java.awt.image.ImageFilter;
  43. import java.awt.image.CropImageFilter;
  44. import java.awt.image.FilteredImageSource;
  45. import java.net.MalformedURLException;
  46.  
  47. /**
  48.  * An extensible ImageMap applet class.
  49.  * The active areas on the image are controlled by ImageArea classes
  50.  * that can be dynamically loaded over the net.
  51.  *
  52.  * @author     Jim Graham
  53.  * @version     1.12, 03/23/98
  54.  */
  55. public class ImageMap 
  56.     extends Applet 
  57.     implements Runnable, MouseListener, MouseMotionListener {
  58.     /**
  59.      * The unhighlighted image being mapped.
  60.      */
  61.     Image baseImage;
  62.  
  63.     /**
  64.      * The list of image area handling objects;
  65.      */
  66.     ImageMapArea areas[];
  67.  
  68.     /**
  69.      * The primary highlight mode to be used.
  70.      */
  71.     static final int BRIGHTER = 0;
  72.     static final int DARKER = 1;
  73.  
  74.     int hlmode = BRIGHTER;
  75.  
  76.     /**
  77.      * The percentage of highlight to apply for the primary highlight mode.
  78.      */
  79.     int hlpercent = 50;
  80.  
  81.     /**
  82.      * The MediaTracker for loading and constructing the various images.
  83.      */
  84.     MediaTracker tracker;
  85.  
  86.     /**
  87.      * Get a rectangular region of the baseImage highlighted according to
  88.      * the primary highlight specification.
  89.      */
  90.     Image getHighlight(int x, int y, int w, int h) {
  91.     return getHighlight(x, y, w, h, hlmode, hlpercent);
  92.     }
  93.  
  94.     /**
  95.      * Get a rectangular region of the baseImage with a specific highlight.
  96.      */
  97.     Image getHighlight(int x, int y, int w, int h, int mode, int percent) {
  98.     return getHighlight(x, y, w, h, new HighlightFilter(mode == BRIGHTER,
  99.                                 percent));
  100.     }
  101.  
  102.     /**
  103.      * Get a rectangular region of the baseImage modified by an image filter.
  104.      */
  105.     Image getHighlight(int x, int y, int w, int h, ImageFilter filter) {
  106.     ImageFilter cropfilter = new CropImageFilter(x, y, w, h);
  107.     ImageProducer prod = new FilteredImageSource(baseImage.getSource(),
  108.                              cropfilter);
  109.     return makeImage(prod, filter, 0);
  110.     }
  111.  
  112.     /**
  113.      * Make a filtered image based on another image.
  114.      */
  115.     Image makeImage(Image orig, ImageFilter filter) {
  116.     return makeImage(orig.getSource(), filter);
  117.     }
  118.  
  119.     /**
  120.      * Make a filtered image based on another ImageProducer.
  121.      */
  122.     Image makeImage(ImageProducer prod, ImageFilter filter) {
  123.     return makeImage(prod, filter,
  124.              (prod == baseImage.getSource()) ? 1 : 0);
  125.     }
  126.  
  127.     /**
  128.      * Make a filtered image based on another ImageProducer.
  129.      * Add it to the media tracker using the indicated ID.
  130.      */
  131.     Image makeImage(ImageProducer prod, ImageFilter filter, int ID) {
  132.     Image filtered = createImage(new FilteredImageSource(prod, filter));
  133.     tracker.addImage(filtered, ID);
  134.     return filtered;
  135.     }
  136.  
  137.     /**
  138.      * Add an image to the list of images to be tracked.
  139.      */
  140.     void addImage(Image img) {
  141.     tracker.addImage(img, 1);
  142.     }
  143.  
  144.     /**
  145.      * Parse a string representing the desired highlight to be applied.
  146.      */
  147.     void parseHighlight(String s) {
  148.     if (s == null) {
  149.         return;
  150.     }
  151.     if (s.startsWith("brighter") || s.startsWith("BRIGHTER")) {
  152.         hlmode = BRIGHTER;
  153.         if (s.length() > "brighter".length()) {
  154.         hlpercent = Integer.parseInt(s.substring("brighter".length()));
  155.         }
  156.     } else if (s.startsWith("darker") || s.startsWith("DARKER")) {
  157.         hlmode = DARKER;
  158.         if (s.length() > "darker".length()) {
  159.         hlpercent = Integer.parseInt(s.substring("darker".length()));
  160.         }
  161.     }
  162.     }
  163.  
  164.     /**
  165.      * Initialize the applet. Get attributes.
  166.      *
  167.      * Initialize the ImageAreas.
  168.      * Each ImageArea is a subclass of the class ImageArea, and is
  169.      * specified with an attribute of the form:
  170.      *         areaN=ImageAreaClassName,arguments...
  171.      * The ImageAreaClassName is parsed off and a new instance of that
  172.      * class is created.  The initializer for that class is passed a
  173.      * reference to the applet and the remainder of the attribute
  174.      * string, from which the class should retrieve any information it
  175.      * needs about the area it controls and the actions it needs to
  176.      * take within that area.
  177.      */
  178.     public void init() {
  179.     String s;
  180.  
  181.     tracker = new MediaTracker(this);
  182.     parseHighlight(getParameter("highlight"));
  183.     introTune = getParameter("startsound");
  184.     baseImage = getImage(getDocumentBase(), getParameter("img"));
  185.     Vector areaVec = new Vector();
  186.     int num = 1;
  187.     while (true) {
  188.         ImageMapArea newArea;
  189.         s = getParameter("area"+num);
  190.         if (s == null) {
  191.         // Try rect for backwards compatibility.
  192.         s = getParameter("rect"+num);
  193.         if (s == null) {
  194.             break;
  195.         }
  196.         try {
  197.             newArea = new HighlightArea();
  198.             newArea.init(this, s);
  199.             areaVec.addElement(newArea);
  200.             String url = getParameter("href"+num);
  201.             if (url != null) {
  202.             s += "," + url;
  203.             newArea = new LinkArea();
  204.             newArea.init(this, s);
  205.             areaVec.addElement(newArea);
  206.             }
  207.         } catch (Exception e) {
  208.             System.out.println("error processing: "+s);
  209.             e.printStackTrace();
  210.             break;
  211.         }
  212.         } else {
  213.         try {
  214.             int classend = s.indexOf(",");
  215.             String name = s.substring(0, classend);
  216.             newArea = (ImageMapArea) Class.forName(name).newInstance();
  217.             s = s.substring(classend+1);
  218.             newArea.init(this, s);
  219.             areaVec.addElement(newArea);
  220.         } catch (Exception e) {
  221.             System.out.println("error processing: "+s);
  222.             e.printStackTrace();
  223.             break;
  224.         }
  225.         }
  226.         num++;
  227.     }
  228.     areas = new ImageMapArea[areaVec.size()];
  229.     areaVec.copyInto(areas);
  230.     checkSize();
  231.     addMouseListener(this);
  232.     addMouseMotionListener(this);
  233.     }
  234.  
  235.     public void destroy() {
  236.         removeMouseListener(this);
  237.         removeMouseMotionListener(this);
  238.     }
  239.  
  240.     Thread aniThread = null;
  241.     String introTune = null;
  242.  
  243.     public void start() {
  244.     if (introTune != null)
  245.         try {
  246.         play(new URL(getDocumentBase(), introTune));
  247.         } catch (MalformedURLException e) {}
  248.     if (aniThread == null) {
  249.             aniThread = new Thread(this);
  250.             aniThread.setName("ImageMap Animator");
  251.             aniThread.start();
  252.     }
  253.     }
  254.  
  255.     public void run() {
  256.     Thread me = Thread.currentThread();
  257.     tracker.checkAll(true);
  258.     for (int i = areas.length; --i >= 0; ) {
  259.         areas[i].getMedia();
  260.     }
  261.     me.setPriority(Thread.MIN_PRIORITY);
  262.     while (aniThread == me) {
  263.         boolean animating = false;
  264.         for (int i = areas.length; --i >= 0; ) {
  265.         animating = areas[i].animate() || animating;
  266.         }
  267.         try {
  268.         synchronized(this) {
  269.             wait(animating ? 100 : 0);
  270.         }
  271.         } catch (InterruptedException e) {
  272.         break;
  273.         }
  274.     }
  275.     }
  276.  
  277.     public synchronized void startAnimation() {
  278.     notify();
  279.     }
  280.  
  281.     public synchronized void stop() {
  282.     aniThread = null;
  283.     notify();
  284.     for (int i = 0; i < areas.length; i++) {
  285.         areas[i].exit();
  286.     }
  287.     }
  288.  
  289.     /**
  290.      * Check the size of this applet while the image is being loaded.
  291.      */
  292.     void checkSize() {
  293.     int w = baseImage.getWidth(this);
  294.     int h = baseImage.getHeight(this);
  295.     if (w > 0 && h > 0) {
  296.         resize(w, h);
  297.         synchronized(this) {
  298.         fullrepaint = true;
  299.         }
  300.         repaint(0, 0, w, h);
  301.     }
  302.     }
  303.  
  304.     private boolean fullrepaint = false;
  305.     private final static long UPDATERATE = 100;
  306.  
  307.     /**
  308.      * Handle updates from images being loaded.
  309.      */
  310.     public boolean imageUpdate(Image img, int infoflags,
  311.                    int x, int y, int width, int height) {
  312.     if ((infoflags & (WIDTH | HEIGHT)) != 0) {
  313.         checkSize();
  314.     }
  315.     if ((infoflags & (SOMEBITS | FRAMEBITS | ALLBITS)) != 0) {
  316.         synchronized(this) {
  317.         fullrepaint = true;
  318.         }
  319.         repaint(((infoflags & (FRAMEBITS | ALLBITS)) != 0)
  320.             ? 0 : UPDATERATE,
  321.             x, y, width, height);
  322.     }
  323.     return (infoflags & (ALLBITS | ERROR)) == 0;
  324.     }
  325.  
  326.     /**
  327.      * Paint the image and all active highlights.
  328.      */
  329.     public void paint(Graphics g) {
  330.     synchronized(this) {
  331.         fullrepaint = false;
  332.     }
  333.     if (baseImage == null) {
  334.         return;
  335.     }
  336.     g.drawImage(baseImage, 0, 0, this);
  337.     if (areas != null) {
  338.         for (int i = areas.length; --i >= 0; ) {
  339.         areas[i].highlight(g);
  340.         }
  341.     }
  342.     }
  343.  
  344.     /**
  345.      * Update the active highlights on the image.
  346.      */
  347.     public void update(Graphics g) {
  348.     boolean full;
  349.     synchronized(this) {
  350.         full = fullrepaint;
  351.     }
  352.     if (full) {
  353.         paint(g);
  354.         return;
  355.     }
  356.     if (baseImage == null) {
  357.         return;
  358.     }
  359.     g.drawImage(baseImage, 0, 0, this);
  360.     if (areas == null) {
  361.         return;
  362.     }
  363.     // First unhighlight all of the deactivated areas
  364.     for (int i = areas.length; --i >= 0; ) {
  365.         areas[i].highlight(g);
  366.     }
  367.     }
  368.  
  369.       int pressX;
  370.       int pressY;
  371.       
  372.   public void mouseClicked(MouseEvent e)
  373.   {}
  374.       
  375.       /**
  376.        * Inform all active ImageAreas of a mouse press.
  377.        */  
  378.   public void mousePressed(MouseEvent e)
  379.   {
  380.     pressX = e.getX();
  381.     pressY = e.getY();
  382.     
  383.     for (int i = 0; i < areas.length; i++) {
  384.       if (areas[i].inside(pressX, pressY)) {
  385.     if (areas[i].press(pressX, pressY)) {
  386.       break;
  387.     }
  388.       }
  389.     }
  390.     e.consume();
  391.   }
  392.  
  393.       /**
  394.        * Inform all active ImageAreas of a mouse release.
  395.        * Only those areas that were inside the original mousePressed()
  396.        * are informed of the mouseReleased.
  397.        */      
  398.   public void mouseReleased(MouseEvent e)
  399.   {
  400.     for (int i = 0; i < areas.length; i++) {
  401.       if (areas[i].inside(pressX, pressY)) {
  402.     if (areas[i].lift(e.getX(), e.getY())) {
  403.       break;
  404.     }
  405.       }
  406.     }
  407.     e.consume();
  408.   }
  409.       
  410.   public void mouseEntered(MouseEvent e)
  411.   {}
  412.       
  413.       /**
  414.        * Make sure that no ImageAreas are highlighted.
  415.        */
  416.   public void mouseExited(MouseEvent e) {    
  417.     for (int i = 0; i < areas.length; i++) {
  418.       areas[i].checkExit();
  419.     }
  420.     e.consume();
  421.   }
  422.  
  423.  
  424.       /**
  425.        * Inform all active ImageAreas of a mouse drag.
  426.        * Only those areas that were inside the original mouseDown()
  427.        * are informed of the mouseUp.
  428.        */      
  429.  
  430.   public void mouseDragged(MouseEvent e)
  431.   { 
  432.     mouseMoved(e);
  433.     for (int i = 0; i < areas.length; i++) {
  434.       if (areas[i].inside(pressX, pressY)) {
  435.     if (areas[i].drag(e.getX(), e.getY())) {
  436.       break;
  437.     }
  438.       }
  439.     }
  440.     e.consume();
  441.   }
  442.       
  443.       /**
  444.        * Find the ImageAreas that the mouse is in.
  445.        */
  446.   public void mouseMoved(MouseEvent e) {
  447.     boolean eaten = false;
  448.     
  449.     for (int i = 0; i < areas.length; i++) {
  450.       if (!eaten && areas[i].inside(e.getX(), e.getY())) {
  451.     eaten = areas[i].checkEnter(e.getX(), e.getY());
  452.       } else {
  453.     areas[i].checkExit();
  454.       }
  455.     }
  456.     e.consume();
  457.   }
  458.  
  459.     /**
  460.      * Scan all areas looking for the topmost status string.
  461.      */
  462.     public void newStatus() {
  463.     String msg = null;
  464.     for (int i = 0; i < areas.length; i++) {
  465.         msg = areas[i].getStatus(msg);
  466.     }
  467.     showStatus(msg);
  468.     }
  469.  
  470.   public String getAppletInfo() {
  471.     return "Title: ImageMap \nAuthor: Jim Graham \nAn extensible ImageMap applet class. \nThe active areas on the image are controlled by ImageArea \nclasses that can be dynamically loaded over the net.";
  472.   }
  473.   
  474.   public String[][] getParameterInfo() {
  475.     String[][] info = {
  476.       {"area[n]", "delimited string", "This parameter takes the form of <ImageAreaClassName>, <ul>, <ur>, <ll>, <lr>, <action> where ImageAreaClassName is the name of the class from which this feedback area is controlled, the next four arguments are the four corners of the feedback zone, and the final argument is that action that should be taken on click or mouseover.  That action can be 1) display text in the status bar (if you provide a string argument), 2) play a sound (if you provide the path to a sound file), or 3) load a page (if you provide a URL)."},
  477.       {"rect[n]", "delimited string", "Deprecated: use area[n]"},
  478.       {"href[n]", "URL string", "Pass in a URL to create a LinkArea which will point to this URL.  Not used in these examples."},
  479.       {"highlight", "string/int", "Pass the word 'brighter' followed by an integer 'n' to change the highlight mode to brighter and the hightlight percentage to n.  Pass the word 'darker' followed by an integer 'm' to change the highlight mode to darker and the highlight percentage to m.  Anything else will be ignored.  The default highlight mode is BRIGHTER and the default highlight percentage is 50."},
  480.       {"startsound", "path string", "The path of a soundclip to play when the image is first displayed."},
  481.       {"img", "path string", "The path to the image to be displayed as a live feedback image map."}
  482.     };
  483.     return info;
  484.   }
  485. }
  486.  
  487.  
  488.  
  489.